<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Member extends CI_Controller {
	
	function __construct()
    {
		// Call the Model constructor
        parent::__construct();
		
		if($this->session->userdata('login') === FALSE)
			redirect(base_url());
    }
	
	public function index()
	{
		$this->load->model('model_kajian');
		$this->load->model('model_message');
		
		$id = $this->session->userdata('uid');
		
		if(isset($_POST['data'])){
			
			$data = $_POST['data'];
			
			$data['text'] = strip_tags($data['text'], '<b><i><strong>');
			$data['user_id'] = $id;
			$data['created'] = date('Y-m-d H:i:s');
			$data['published'] = 'yes';
			$data['parent_id'] = isset($data['parent_id'])?$data['parent_id']:'0';
			$data['url']='';
			$data['title']='';
			$data['description']='';
			$data['image']='';
			
			$regexp = '/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/';
			preg_match($regexp,$data['text'],$url);
			
			if(count($url) > 0){
				
				$data['text'] = str_replace($url[0],'<a href="'.$url[0].' target="_blank">'.$url[0].'</a>',$data['text']);
				
				$string = fetch_record($url[0]);
				
				if($string != ""){
					/// fecth title
					$title_regex = "/<title>(.+)<\/title>/i";
					preg_match_all($title_regex, $string, $title, PREG_PATTERN_ORDER);
					$url_title = $title[1];

					/// fecth decription
					//$tags = get_meta_tags($url[0]);
					preg_match('/name\="description" content\="([^"]*)"/iU', $string, $tags);
					
					// fetch images
					$image_regex = '/<img[^>]*'.'src=[\"|\'](.*)[\"|\']/Ui';
					preg_match_all($image_regex, $string, $img, PREG_PATTERN_ORDER);
					$images_array = $img[1];
					
					for ($i=0;$i<=sizeof($images_array);$i++)
					{
						if(@$images_array[$i])
						{
							if(@getimagesize(@$images_array[$i]))
							{
								list($width, $height, $type, $attr) = getimagesize(@$images_array[$i]);
								if($width >= 50 && $height >= 50 ){				
									$data['image'] = @$images_array[$i];
									break;
								}
							}
						}
					}
					
					
					if(isset($url_title[0])){
						$data['url'] = $url[0];
						$data['title'] = $url_title[0];
						$data['description'] = @$tags[1];//@$tags['description'];
					}
				}
			}
			
			// var_dump($data);
			//var_dump($url);

			$id = $this->model_message->add($data);
			
			$query = $this->model_message->get_by($id);
			
			$respon = array(
				'status' => 'success',
				'text' => '',
				'url' => '',
				'title' => '',
				'description' => '',
				'image' => '',
				'created' => date("Y-m-d H:i:s"),
				'time_link' => DateToIndo(date("Y-m-d H:i:s"))
			);
			
			foreach($query->result_array() as $row){
				$respon['id'] = $row['id'];
				$respon['nama_user'] = $row['nama_user'];
				$respon['profile_image'] = $row['profile_image'];
				$respon['text'] = nl2br($row['text']);
				$respon['url'] = $row['url'];
				$respon['title'] = $row['title'];
				$respon['description'] = substr($row['description'],0,250);
				$respon['image'] = $row['image'];
				$respon['created'] = $row['created'];
				$respon['time_link'] = DateToIndo($row['created']);
			}
							
			echo $msg = json_encode($respon);
			return;
		}
		
		$data['kajian'] = $this->model_kajian->sort_kajian(0,30);
		$data['feed'] = $this->model_message->get_all();
		$sub['sub'] = $this->load->view('member/favorit/info',$data,TRUE);
		$view['content'] = $this->load->view('member/frame',$sub,TRUE);
		$this->load->view('index',$view);
	}

	function post($id){
		$this->load->model('model_message');
		
		$data['feed'] = $this->model_message->get_detail($id);
		$sub['sub'] = $this->load->view('member/favorit/info_detail',$data,TRUE);
		$view['content'] = $this->load->view('member/frame',$sub,TRUE);
		$this->load->view('index',$view);
	}
	
	function comment(){
		$this->load->model('model_message');
		
		$id = isset($_POST['id'])?$_POST['id']:0;
		$date = isset($_POST['time'])?$_POST['time']:0;
		
		$comm = $this->model_message->get_comment($id,$date);
		
		echo json_encode($comm->result_array());
	}
	
	function feed(){
		$this->load->model('model_message');
		$time = $_POST['time'];
		$top = $_POST['top'] == 'true'?true:false;
		
		$comm = $this->model_message->get_feed(0,10,$time,$top);
		
		echo json_encode($comm);
	}
}
